package br.com.puertorico.model.service.utils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * Classe utilizada para indicar o progresso de uma terafa.
 *
 * @author daniel
 *
 */
public class ProgressIndicator implements Serializable {

	private static final long serialVersionUID = -1435357314518987322L;

	private Integer current;

	private Integer max;

	private ProgressIndicatorStatusEnum status;

	public ProgressIndicator() {
		status = ProgressIndicatorStatusEnum.WAITING;
	}

	public Integer getCurrent() {
		return current;
	}

	public void setCurrent(Integer current) {
		this.current = current;
	}

	public Integer getMax() {
		return max;
	}

	public void setMax(Integer max) {
		this.max = max;
	}

	public Integer getProgress() {
		// nesse caso o progresso sera 0%
		if (status != ProgressIndicatorStatusEnum.RUNNING) {
			return 0;
		}
		BigDecimal hundred = new BigDecimal(100);
		BigDecimal max = new BigDecimal(this.max);
		BigDecimal current = new BigDecimal(this.current);
		BigDecimal rate = current.multiply(hundred).divide(max,
				RoundingMode.HALF_UP);
		Integer rateInt = rate.intValue() > 100 ? 100 : rate.intValue();
		if (rateInt >= 100) {
			return 100;
		}
		return rateInt;
	}

	public void prepare() {
		status = ProgressIndicatorStatusEnum.PREPARING;
	}

	public void run() {
		status = ProgressIndicatorStatusEnum.RUNNING;
	}

	public void cancel() {
		status = ProgressIndicatorStatusEnum.CANCELED;
	}

	public void finish() {
		status = ProgressIndicatorStatusEnum.FINISHED;
	}

	public boolean isPreparing() {
		return status == ProgressIndicatorStatusEnum.PREPARING;
	}

	public boolean isRunning() {
		return status == ProgressIndicatorStatusEnum.RUNNING;
	}

	public boolean isFinished() {
		return status == ProgressIndicatorStatusEnum.FINISHED;
	}

	public boolean isCanceled() {
		return status == ProgressIndicatorStatusEnum.CANCELED;
	}

	public boolean isWaiting() {
		return status == ProgressIndicatorStatusEnum.WAITING;
	}

}